//
//  ProductListTableViewCell.m
//  PPDL
//
//  Created by 王洋 on 2019/9/23.
//  Copyright © 2019 王洋. All rights reserved.
//

#import "ProductListTableViewCell.h"
#import "OYCountDownManager.h"

@implementation ProductListTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
}

// xib创建
- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        // 监听通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(countDownNotification) name:OYCountDownNotification object:nil];
    }
    return self;
}

- (void)dealloc {
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)changeShowStatusEnd {
    
    self.model.isWaitStart = NO;
    self.model.isWaitEnd = NO;
    self.model.isEnd = YES;
    
    self.leaveTime.text = @"已经结束";
    self.itemEndPaiLab.hidden = NO;

    self.model.countBGColor = [UIColor lightGrayColor];
    self.leaveTime.layer.backgroundColor = [UIColor lightGrayColor].CGColor;
}


- (void)changeStatusDistanceToStart {
    
    self.model.isWaitStart = YES;
    self.model.isWaitEnd = NO;
    self.model.isEnd = NO;
    
    self.model.prefixStr = @"距开场:";
    
    self.model.countBGColor = UIColorHex(0x219C00);
    self.leaveTime.layer.backgroundColor = UIColorHex(0x219C00).CGColor;
}

- (void)changeStatusDistanceToEnd {
    
    self.model.isWaitStart = NO;
    self.model.isWaitEnd = YES;
    self.model.isEnd = NO;
    
    self.model.prefixStr = @"距结束:";
    
    self.leaveTime.layer.backgroundColor = UIColorHex(0xFF3430).CGColor;
    self.model.countBGColor = UIColorHex(0xFF3430);
}

#pragma mark 禁止用户交互
- (void)stopUserInteractive {
    self.itemEndPaiLab.hidden = NO;
    self.userInteractionEnabled = NO;
}

//开放用户交互
- (void)openUpUserInteractive {
    self.itemEndPaiLab.hidden = YES;
    self.userInteractionEnabled = YES;
}

#pragma mark - 倒计时通知回调
- (void)countDownNotification {
    
    /// 已经结束直接return
    if (self.model.isEnd) {
        
        [self changeShowStatusEnd];
        [self stopUserInteractive];

        return;
    }
    
    //刷新当前cell的model
    [self refreshModel];
    
    //判断状态
    if (self.model.isWaitStart) {
        
        [self changeStatusDistanceToStart];
        
        //由等待开场到等待结束
        if (self.model.count <= 0) {
            
            self.model.count = self.model.distanceToEnd;//等待结束    倒计时为   结束时间减去当前时间
            
            [self changeStatusDistanceToEnd];
            
            if (self.countDownZero) {
                self.countDownZero(self.model);
            }
        }
    }
    
    if (self.model.isWaitEnd) {    //处于等待结束状态
        
        [self changeStatusDistanceToEnd];
        
        //由等待结束到已结束
        if (self.model.count <= 0) {
            
            [self changeShowStatusEnd];
            [self stopUserInteractive];

            if (self.countDownZero) {
                self.countDownZero(self.model);
            }
        }
    }
    
    
    NSString *time =  [NSString stringWithFormat:@"%@ %02ld:%02ld:%02ld",self.model.prefixStr, self.model.count/3600, (self.model.count/60)%60, self.model.count%60];
    self.leaveTime.text = time;
}

- (void)refreshModel {
    
    NSTimeInterval currentTimeStamp = [Utilities getCurrentServerTimestamp];

    if (currentTimeStamp < self.model.beginTimestamp) {
        
        self.model.isWaitStart = YES;
        self.model.isWaitEnd = NO;
        self.model.isEnd = NO;
        
        self.model.distanceToStart = (self.model.beginTimestamp - currentTimeStamp)/1000;
        self.model.distanceToEnd = (self.model.endTimestamp - currentTimeStamp)/1000;
        self.model.count = self.model.distanceToStart;//等待开场    倒计时为   开始时间减去当前时间
        
    } else if (currentTimeStamp > self.model.beginTimestamp && currentTimeStamp < self.model.endTimestamp) {
        
        self.model.isWaitStart = NO;
        self.model.isWaitEnd = YES;
        self.model.isEnd = NO;
        
        self.model.distanceToEnd = (self.model.endTimestamp - currentTimeStamp)/1000;
        self.model.count = self.model.distanceToEnd;//等待结束    倒计时为   结束时间减去当前时间
        
    } else if (currentTimeStamp > self.model.endTimestamp) {
        
        self.model.isWaitStart = NO;
        self.model.isWaitEnd = NO;
        self.model.isEnd = YES;
        
        self.model.count = 0;
        
    } else {
        
    }
}

- (void)fillCellWithModel:(HomeProductItemModel *)model {
    
    self.model = model;
    [self.productImage sd_setImageWithURL:[NSURL URLWithString:model.productImage] placeholderImage:[UIImage imageNamed:@"placeholder_rect"] options:SDWebImageRetryFailed];
    self.productName.text = model.productName;
    self.leaveTime.layer.backgroundColor = self.model.countBGColor.CGColor;
    
    if (self.model.isEnd) {
        [self stopUserInteractive];
    } else {
        [self openUpUserInteractive];
    }
    
    self.auctionPrice.text = [NSString stringWithFormat:@"¥ %@",[Utilities changeDoubleToString:model.auctionPrice]];
    self.marketPrice.text =[NSString stringWithFormat:@"市场价：¥ %@",[Utilities changeDoubleToString:model.marketPrice]];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.cellItemBGView pp_clipCorners:UIRectCornerAllCorners radius:8];
        [self.qipaijiaLab pp_clipCorners:UIRectCornerAllCorners radius:8];
        self.cellItemBGView.clipsToBounds = YES;
    });

}

@end
